#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

void SetNonBlock(int fd)
{
    int f1 = fcntl(fd, F_GETFL);
    if(f1 < 0)
    {
        perror("fcntl");
        return;
    }
    fcntl(fd, F_SETFL, f1 | O_NONBLOCK);
}
int main()
{
    SetNonBlock(0);
    while(1)
    {
        char buffer[1024];
        ssize_t s = read(0, buffer, sizeof(buffer)-1);
        if(s > 0)
        {
            buffer[s] = 0;
            write(1, buffer, strlen(buffer));
            printf("read success, s: %d, errno : %d\n", s, errno);
        }
        else
        {
            if(errno == EAGAIN || errno == EWOULDBLOCK)
            {
                printf("数据没有准备好，再试试吧！\n");
                printf("read fail, s: %d, errno = %d\n", s, errno);
                //做做其他事情
                sleep(2);
                continue;
            }
        }
    }
    return 0;
}
